home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / GCC / V2-4-5 / GPPLIBSR00 / cc / stdiostrea < prev    next >
Text File  |  1993-12-08  |  3KB  |  113 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifdef __GNUG__
  19. #pragma implementation "stdiostream.h"
  20. #endif
  21.  
  22.  
  23. #include "stdiostream.h"
  24.  
  25. // A stdiobuf is "tied" to a FILE object (as used by the stdio package).
  26. // Thus a stdiobuf is always synchronized with the corresponding FILE,
  27. // though at the cost of some overhead.  (If you use the implementation
  28. // of stdio supplied with this library, you don't need stdiobufs.)
  29. // This implementation inherits from filebuf, but implement the virtual
  30. // functions sys_read/..., using the stdio functions fread/... instead
  31. // of the low-level read/... system calls.  This has the advantage that
  32. // we get all of the nice filebuf semantics automatically, though
  33. // with some overhead.
  34.  
  35.  
  36. #ifndef SEEK_SET
  37. #define SEEK_SET 0
  38. #endif
  39. #ifndef SEEK_CUR
  40. #define SEEK_CUR 1
  41. #endif
  42. #ifndef SEEK_END
  43. #define SEEK_END 2
  44. #endif
  45.  
  46. stdiobuf::stdiobuf(FILE *f) : filebuf(fileno(f))
  47. {
  48.     _file = f;
  49.     // Turn off buffer in stdiobuf.  Instead, rely on buffering in (FILE).
  50.     // Thus the stdiobuf will be synchronized with the FILE.
  51.     setbuf(NULL, 0);
  52. }
  53.  
  54. _G_ssize_t stdiobuf::sys_read(char* buf, size_t size)
  55. {
  56.     return fread(buf, 1, size, _file);
  57. }
  58.  
  59. _G_ssize_t stdiobuf::sys_write(const void *buf, long n)
  60. {
  61.     return fwrite(buf, 1, n, _file);
  62.     if (_fb._offset >= 0)
  63.     _fb._offset += n;
  64. }
  65.  
  66. _G_fpos_t stdiobuf::sys_seek(_G_fpos_t offset, _seek_dir dir)
  67. {
  68.     // Normally, equivalent to: fdir=dir
  69.     int fdir =
  70.     (dir == ios::beg) ? SEEK_SET :
  71.         (dir == ios::cur) ? SEEK_CUR :
  72.         (dir == ios::end) ? SEEK_END :
  73.         dir;
  74.     return fseek(_file, offset, fdir);
  75. }
  76.  
  77. int stdiobuf::sys_close()
  78. {
  79.     int status = fclose(_file);
  80.     _file = NULL;
  81.     return status;
  82. }
  83.  
  84. int stdiobuf::sync()
  85. {
  86.     if (filebuf::sync() == EOF)
  87.     return EOF;
  88.     if (!(xflags() & _S_NO_WRITES))
  89.     if (fflush(_file))
  90.         return EOF;
  91. #if 0
  92.     // This loses when writing to a pipe.
  93.     if (fseek(_file, 0, SEEK_CUR) == EOF)
  94.     return EOF;
  95. #endif
  96.     return 0;
  97. }
  98.  
  99. int stdiobuf::overflow(int c /* = EOF*/)
  100. {
  101.     if (filebuf::overflow(c) == EOF)
  102.     return EOF;
  103.     if (c != EOF)
  104.     return c;
  105.     return fflush(_file);
  106. }
  107.  
  108. int stdiobuf::xsputn(const char* s, int n)
  109. {
  110.     // The filebuf implementation of sputn loses.
  111.     return streambuf::xsputn(s, n);
  112. }
  113.